package com.ggh.jinjilive.ui.home.video.adapter;

import android.content.Context;
import android.graphics.Color;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import androidx.annotation.NonNull;
import androidx.databinding.DataBindingUtil;
import androidx.recyclerview.widget.RecyclerView;

import com.ggh.jinjilive.R;
import com.ggh.jinjilive.databinding.ItemLiveTypeBinding;
import com.ggh.jinjilive.listener.AddItemOnClickListener;
import com.ggh.jinjilive.mobile.home.live.LiveCategory;
import com.ggh.jinjilive.remotedata.BaseApiModule;
import com.bumptech.glide.Glide;

import java.util.List;

public class VideoTypeAdapter extends RecyclerView.Adapter<VideoTypeAdapter.Holder> {
    private Context mContext;
    private List<LiveCategory> liveCategoryList;
    private int selected = 0;

    public VideoTypeAdapter(Context mContext) {
        this.mContext = mContext;
    }

    public void setAdd(List<LiveCategory> liveCategoryList) {
        this.liveCategoryList = liveCategoryList;
        notifyDataSetChanged();
    }

    public void Add(List<LiveCategory> liveCategoryList) {
        if (null != this.liveCategoryList) {
            this.liveCategoryList.addAll(liveCategoryList);
        } else {
            this.liveCategoryList = liveCategoryList;
        }
        notifyDataSetChanged();
    }


    public void setSelected(int selected) {
        this.selected = selected;
        notifyDataSetChanged();
    }


    private AddItemOnClickListener addItemOnClickListener;

    public void setAddItemOnClickListener(AddItemOnClickListener addItemOnClickListener) {
        this.addItemOnClickListener = addItemOnClickListener;
    }

    @NonNull
    @Override
    public Holder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        ItemLiveTypeBinding binding = DataBindingUtil.inflate(LayoutInflater.from(parent.getContext()), R.layout.item_live_type, parent, false);
        return new Holder(binding);
    }

    @Override
    public void onBindViewHolder(@NonNull Holder holder, int position) {
        LiveCategory liveCategory = liveCategoryList.get(position);
        if (null != liveCategory) {
            if (selected == position) {
                Glide.with(holder.binding.image.getContext()).load(BaseApiModule.host + liveCategory.getIcon_select()).into(holder.binding.image);
                holder.binding.text.setTextColor(Color.parseColor("#7FA8FC"));
            } else {
                Glide.with(holder.binding.image.getContext()).load(BaseApiModule.host + liveCategory.getIcon_unselect()).into(holder.binding.image);
                holder.binding.text.setTextColor(Color.parseColor("#FFFFFF"));
            }
            holder.binding.text.setText(liveCategory.getCategory_name());
            holder.binding.rootLayout.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    setSelected(position);
                    if (null != addItemOnClickListener) {
                        addItemOnClickListener.onSkipClicked(position, liveCategory);
                    }
                }
            });
        }
    }

    @Override
    public int getItemCount() {
        return null == liveCategoryList ? 0 : liveCategoryList.size();
    }

    public class Holder extends RecyclerView.ViewHolder {
        ItemLiveTypeBinding binding;

        public Holder(@NonNull ItemLiveTypeBinding binding) {
            super(binding.getRoot());
            this.binding = binding;
        }
    }
}